﻿$.extend($.fn.treegrid.methods, {
	cancelEditing:function(jq){
		var dg=jq;
		var editId = dg.data('editId');
		if (editId){
			dg.treegrid('cancelEdit',editId);
		}
	},
	beginEditing:function(jq,param){
		var dg=jq;
		var idField=dg.treegrid('options').idField;
		var rowid=param[idField];
		if(dg.data('editId')!=rowid){
    		if(dg.treegrid('endEditing')){
    			//开始编辑
    			dg.treegrid('beginEdit', rowid);
				dg.data('editId',rowid);
    		}
    	}
	},
	endEditing:function(jq){
		var dg=jq;
		var editId = dg.data('editId');
		var editIndex=dg.treegrid('getRowIndex',editId);
		if (editIndex == undefined){return true;}
		if (dg.treegrid('validateRow', editIndex)){
			dg.treegrid('endEdit', editId);
			dg.removeData('editId');
			return true;
		} else {
			return false;
		}
	},
	appendNode:function(jq,param){
		return jq.each(function(){
			var dg=jq;
			if(dg.treegrid('endEditing')){
				var idField=dg.treegrid('options').idField;
				var parentIdField=dg.treegrid('options').parentIdField;
				dg.treegrid('append',{
					parent: param[parentIdField],  // the node has a 'id' value that defined through 'idField' property
					data: [param]
				});
				dg.data('editId',param[idField]);
				dg.treegrid('beginEdit',param[idField]);//编辑新增行
			}
		});
	},
	editRow:function(jq,param){
		var dg=jq;
		var selectRows=dg.treegrid('getSelections');
		if(selectRows.length>0){
			dg.treegrid('beginEditing',selectRows[0]);
		}else{
			$.messager.alert("提示", "请选择一条数据！");
		}
	},
	removeNode:function(jq){
		var dg=jq;
		var selectRow=dg.treegrid('getSelections');
		var idField=dg.treegrid('options').idField;
		if(selectRow.length>0){
			$.each(selectRow,function(i,v){
				dg.treegrid('remove', v[idField]);
			});
		}
	},
	getEditIndex:function(jq){
		var dg=jq;
		var editIndex=undefined;
		var editId = dg.data('editId');
		if (editId){
			editIndex=dg.treegrid('getRowIndex',editId);
		}
		return editIndex;
	},
	load:function(jq,param){
		var dg=jq;
		dg.treegrid("loading");
		var searchbar=dg.treegrid('options').toolbar;
		var table=dg.treegrid('options').table;
		var sqlname=dg.treegrid('options').sqlname;
		var searchForm=$(searchbar+" form");
		var searchParam=searchForm.serializeObject();
		var idField=dg.treegrid('options').idField;
		var parentIdField=dg.treegrid('options').parentIdField;
		$.extend(searchParam,dg.gridParam);
		$.post("/authenticated/grid/"+sqlname,searchParam,function(result){
			dg.treegrid("loadData",transformCombotreeArray(result.rows,idField,parentIdField));
			if(dg.pager){
				dg.treegrid('getPager').pagination('refresh',{
					total: result.total,
					pageNumber: dg.gridParam.pageNumber
				});
			}
			dg.treegrid("loaded");
		});
		
	},accept:function(jq,param){
		var dg=jq;
		var table=dg.treegrid('options').table;
		var sqlname=dg.treegrid('options').sqlname;
		if (dg.treegrid('endEditing')){
			var rows = dg.treegrid('getChanges');
			if (rows.length == 0) {
				$.messager.alert("提示", "没有需要提交的数据！");
				return;
			}
			var insertedRows = dg.treegrid('getChanges','inserted');
			var deletedRows  = dg.treegrid('getChanges','deleted');
			var updatedRows  = dg.treegrid('getChanges','updated');
			var params = {};
			if(insertedRows.length>0){
				params.insertedRows=insertedRows;
			}
			if(updatedRows.length>0){
				params.updatedRows=updatedRows;
			}
			if(deletedRows.length>0){
				var idArr=new Array();
				var idField=dg.treegrid('options').idField;
				$.each(deletedRows, function(i, row){
					idArr.push(row[idField]);
				});
				params.deletedRowIds=idArr;
			}
			$.post("/authenticated/grid/update/treeGrid/"+sqlname,params,function(result){
				if(result.success){
					dg.treegrid('acceptChanges');
					dg.treegrid('load');
					$.messager.alert("提示", "保存成功！");
				}else{
					$.messager.alert("错误", result.message);
				}			
			});
		}
	},showChanges:function(jq,param){
		var dg=jq;
		if (dg.treegrid('endEditing')){
			var changesRows=dg.treegrid('getChanges');
			var insertedRows = dg.treegrid('getChanges','inserted');
			var deletedRows = dg.treegrid('getChanges','deleted');
			var updatedRows = dg.treegrid('getChanges','updated');
			$.messager.alert("提示", changesRows.length+"条数据修改<br>新增"+insertedRows.length+"条,删除"+deletedRows.length+"条，更新"+updatedRows.length+"条");
		}else{
			$.messager.alert("提示", "编辑未结束！");
		}
	},getSelectedRow:function(jq,param){
		var dg=jq;
		var selectRow=dg.treegrid('getSelected');
		if(selectRow){
			return selectRow;
		}else{
			$.messager.alert("提示", $.messager.defaults.selectRow);
			return undefined;
		}
	}
});
uniontrip={
		treegrid:{
			searchEdit:function (data){
				var self = this;
				this.columns=[[]];
				this.fun={
					field:{
						formatter:function(param){
							this.field=param;
							this.comboboxFormatter=function (value,row,index){
								return self.getComoboxValue(this.field,value);
						    };
							this.combotreeFormatter=function (value,row,index){
								return self.getCombotreeValue(this.field,value);
						    };
						}
					}
				};
				this.comboboxDatas={};
				this.combotreeDatas={};
				this.getComoboxValue=function(field,value){
					var text;
					$.each(self.comboboxDatas[field],function(i,v){
						if(v.code_value==value){
							text= v.code_name;
							return text;
						}
					});
					return text;
				};
				this.getCombotreeValue=function(field,value){
					var text;
					$.each(self.comboboxDatas[field],function(i,v){
						if(v.id==value){
							text= v.text;
							return text;
						}
					});
					return text;
				}
				this.onSelectPage=function(pageNumber, pageSize){
					self.grid.gridParam.pageNumber=pageNumber;
					self.grid.gridParam.pageSize=pageSize;
					self.grid.treegrid("load");
				};
				
				this.onDblClickRow=function(rowData){
					self.grid.treegrid('beginEditing',rowData);
				};
				this.onSortColumn=function(sort, order){
					self.grid.gridParam.sort=sort;
					self.grid.gridParam.order=order;
					self.grid.treegrid("load");
				};
				this.editRow=function(){
					self.grid.treegrid('editRow');
				};
				this.accept=function(){
					self.grid.treegrid('accept');
				};
				this.cancelEditing=function(){
					self.grid.treegrid('cancelEditing');
				};
				this.endEditing=function(){
					self.grid.treegrid('endEditing');
				};
				this.rejectChanges=function(){
					//treegrid没有此功能
					self.grid.treegrid('rejectChanges');
				};
				this.remove=function(){
					self.grid.treegrid('removeNode');
				};
				this.load= function(){
					self.grid.treegrid('load');
				};
				this.showChanges = function(){
					self.grid.treegrid('showChanges');
				}
				this.paginationOptions={layout:['list','sep','first','prev','sep','manual','sep','next','last','sep','refresh'],
						pageSize:19,
						pageList:[19,50,100],
					    onSelectPage:this.onSelectPage
				};
				this.grid=$('#'+data.grid_id);
				var isPagination=true;
				//if(data.pager_id){
					//this.grid.pager=$('#'+data.pager_id);
					//this.grid.pager.pagination(this.paginationOptions);
				this.grid.gridParam={"pageNumber":1,"pageSize":this.paginationOptions.pageSize};
				if(data.gridParam){
					$.extend(this.grid.gridParam,data.gridParam);
				}
				//}
				if(data.paging=="no"){
					//不分页
					this.grid.gridParam={"paging":"no"};
					isPagination=false;
				}
				this.gridOptions={
						idField:'id',//指定id字段
						treeField: data.treeField,
						parentIdField:"parent_id",
						sqlname:data.sqlname,
						toolbar:"#searchbar",
						fit:true,
					    singleSelect: true,//单选
						rownumbers:false,//显示序号
						showCheckbox:false,
						pagination:isPagination,//显示分页
						columns:this.columns,//定义显示列
						onSortColumn:this.onSortColumn,//点击排序按钮触发函数
						//onClickRow:this.onClickRow,
						onDblClickRow:this.onDblClickRow,//双击行触发函数
						//selectOnCheck:false,//点击复选框是否选中该行
						//checkOnSelect:false,//点击行时是否选中复选框
						multiSort:true//是否启用多列排序
				};
				if(data.isToolbar=="no"){
					delete this.gridOptions.toolbar;
				}else{
					if(data.toolbar_id){
						this.gridOptions.toolbar="#"+data.toolbar_id;
					}
				}
				//单击事件
				if(data.onClickRow){
					this.gridOptions.onClickRow=data.onClickRow;
				}
				if(data.idField){
					this.gridOptions.idField=data.idField;
				}
				if(data.parentIdField){
					this.gridOptions.parentIdField=data.parentIdField;
				}
				this.initColumnUrl=data.initColumnUrl||"/authenticated/grid/column/v2/"+data.page_id;
				this.initColumn=function(){
					$.ajax({
						async:false,
						url:self.initColumnUrl,
						contentType:"application/json",
						method:"post",
						success:function(resultObject){
							if(resultObject.success){
								var gridColumn=resultObject.data.column;
								if(gridColumn){
									self.comboboxDatas=resultObject.data.combo_data;
									$.each(gridColumn,function(i,v){
										var column_name=v.column_name;
										var column={field:column_name,title:v.column_display_name};
										if(v.is_hidden=="yes"){
											column.hidden=true;
										}
										if(v.is_order=="yes"){
											column.sortable=true;
										}
										if(v.is_edit_column=="yes"){
											var edit_type=v.edit_column_type;
											if(edit_type=="combobox"){
												column.editor={
													type:'combobox',
													options:{
														panelHeight:'auto',
														editable:false,
														valueField:'code_value',
														textField:'code_name',
														data: self.comboboxDatas[column_name]
													}
												};
												column.formatter=new self.fun.field.formatter(column_name).comboboxFormatter;
											}else if(edit_type=="combotree"){
												var newArray=transformCombotreeArray(self.comboboxDatas[column_name]);
												self.combotreeDatas[column_name]=newArray;
												column.editor={
													type:'combotree',
													options:{
														data:newArray
													}
												};
												column.formatter=new self.fun.field.formatter(column_name).combotreeFormatter;
											}else{
												column.editor={
													type:'textbox',options:{}
												};
											}
										}else{
											var edit_type=v.edit_column_type;
											if(edit_type=="image"){
												column.formatter=function(value,row,index){
								            	    
									  				return "<img height=\"80\" src=\""+value+"\" />";
											  	}
											}
										}
										if(v.width&&v.width>0){
											column.width=v.width;
										}
										self.columns[0].push(column);
									});
									
								}
							}else{
								$.messager.alert("提示", "初始化表格失败："+resultObject.message);
							}
						}
					});
					
				}
				if(data.columns){
					this.columns=data.columns;
					this.gridOptions.columns=data.columns;
				}else{
					this.initColumn();
				}
				this.grid.treegrid(this.gridOptions);
				this.grid.pager=this.grid.treegrid('getPager');
				this.grid.pager.pagination(this.paginationOptions);
				this.grid.treegrid("load");
			}
		}
};